package com.minimalist.basic.controller.lindi;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.minimalist.common.config.mybatis.bo.PageResp;
import com.minimalist.common.entity.lindi.vo.contracttemplate.LindiContractTemplateVO;
import com.minimalist.common.entity.lindi.vo.tradeapplicationtemplateconfirmletter.LindiTradeApplicationTemplateConfirmLetterQueryVO;
import com.minimalist.common.entity.lindi.vo.tradeapplicationtemplateconfirmletter.LindiTradeApplicationTemplateConfirmLetterVO;
import com.minimalist.common.feign.client.lindi.LindiTradeApplicationTemplateConfirmLetterFeignClient;
import com.minimalist.common.response.R;
import com.minimalist.common.utils.Add;
import com.minimalist.common.utils.SafetyUtil;
import com.minimalist.common.utils.Update;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 林地指标交易平台_交易申请确认函模板控制器
 *
 * @author 11987
 */
@Validated
@RestController
@Tag(name = "林地指标交易平台_交易申请确认函模板管理")
@RequestMapping("/basic/lindi/tradeApplicationTemplateConfirmLetter")
public class LindiTradeApplicationTemplateConfirmLetterController{

    @Autowired
    private LindiTradeApplicationTemplateConfirmLetterFeignClient feignClient;

    @PostMapping("/addTradeApplicationTemplateConfirmLetter")
    @Operation(summary = "添加交易申请确认函模板")
    @SaCheckPermission("lindi:tradeApplicationTemplateConfirmLetter:add")
    public R<?> addTradeApplicationTemplateConfirmLetter(
            @RequestBody @Validated(Add.class)
            LindiTradeApplicationTemplateConfirmLetterVO tradeApplicationTemplateConfirmLetterVO){
        tradeApplicationTemplateConfirmLetterVO.setCreateId(StpUtil.getLoginIdAsLong());
        tradeApplicationTemplateConfirmLetterVO.setUpdateId(StpUtil.getLoginIdAsLong());
        LocalDateTime now = LocalDateTime.now();
        tradeApplicationTemplateConfirmLetterVO.setCreateTime(now);
        tradeApplicationTemplateConfirmLetterVO.setUpdateTime(now);
        return feignClient.addTradeApplicationTemplateConfirmLetter(tradeApplicationTemplateConfirmLetterVO);
    }

    @DeleteMapping("/deleteTradeApplicationTemplateConfirmLetterByTradeApplicationTemplateConfirmLetterId")
    @Operation(summary = "删除交易申请确认函模板 -> 根据交易申请确认函模板ID删除")
    @SaCheckPermission("lindi:tradeApplicationTemplateConfirmLetter:delete")
    public R<?> deleteTradeApplicationTemplateConfirmLetterByTradeApplicationTemplateConfirmLetterId(
            @RequestParam("tradeApplicationTemplateConfirmLetterId")
            @NotNull(message = "交易申请确认函模板ID不能为空")
            @Parameter(name = "tradeApplicationTemplateConfirmLetterId", required = true, description = "交易申请确认函模板ID")
            Long tradeApplicationTemplateConfirmLetterId){
        return feignClient.deleteTradeApplicationTemplateConfirmLetterByTradeApplicationTemplateConfirmLetterId(
                tradeApplicationTemplateConfirmLetterId);
    }

    @PutMapping("/updateTradeApplicationTemplateConfirmLetterByTradeApplicationTemplateConfirmLetterId")
    @Operation(summary = "修改交易申请确认函模板")
    @SaCheckPermission("lindi:tradeApplicationTemplateConfirmLetter:update")
    public R<?> updateTradeApplicationTemplateConfirmLetterByTradeApplicationTemplateConfirmLetterId(
            @RequestBody @Validated(Update.class)
            LindiTradeApplicationTemplateConfirmLetterVO tradeApplicationTemplateConfirmLetterVO){
        tradeApplicationTemplateConfirmLetterVO.setUpdateId(StpUtil.getLoginIdAsLong());
        tradeApplicationTemplateConfirmLetterVO.setUpdateTime(LocalDateTime.now());
        return feignClient.updateTradeApplicationTemplateConfirmLetterByTradeApplicationTemplateConfirmLetterId(
                tradeApplicationTemplateConfirmLetterVO);
    }

    @GetMapping("/getContractTemplateList")
    @Operation(summary = "获取合同模板列表")
    @SaCheckPermission("lindi:tradeApplicationTemplateContract:get")
    public R<List<LindiContractTemplateVO>> getContractTemplateList(){
        return feignClient.getContractTemplateList();
    }

    @GetMapping("/getPageTradeApplicationTemplateConfirmLetterList")
    @Operation(summary = "查询交易申请确认函模板列表(分页)")
    @SaCheckPermission("lindi:tradeApplicationTemplateConfirmLetter:get")
    public R<PageResp<LindiTradeApplicationTemplateConfirmLetterVO>> getPageTradeApplicationTemplateConfirmLetterList(
            LindiTradeApplicationTemplateConfirmLetterQueryVO queryVO){
        return feignClient.getPageTradeApplicationTemplateConfirmLetterList(queryVO);
    }

    @GetMapping("/getTradeApplicationTemplateConfirmLetterByTradeApplicationTemplateConfirmLetterId/{tradeApplicationTemplateConfirmLetterId}")
    @Operation(summary = "根据交易申请确认函模板ID查询交易申请确认函模板")
    @SaCheckPermission("lindi:tradeApplicationTemplateConfirmLetter:get")
    public R<LindiTradeApplicationTemplateConfirmLetterVO> getTradeApplicationTemplateConfirmLetterByTradeApplicationTemplateConfirmLetterId(
            @PathVariable(value = "tradeApplicationTemplateConfirmLetterId")
            @NotNull(message = "交易申请确认函模板ID不能为空")
            @Parameter(name = "tradeApplicationTemplateConfirmLetterId", description = "交易申请确认函模板ID", required = true)
            Long tradeApplicationTemplateConfirmLetterId){
        return feignClient.getTradeApplicationTemplateConfirmLetterByTradeApplicationTemplateConfirmLetterId(
                tradeApplicationTemplateConfirmLetterId);
    }

    @PostMapping("/deleteTradeApplicationTemplateConfirmLetterFileByFileId")
    @Operation(summary = "删除交易申请确认函模板文件 -> 根据文件ID删除")
    @SaCheckPermission("lindi:tradeApplicationTemplateConfirmLetter:delete")
    public R<?> deleteTradeApplicationTemplateConfirmLetterFileByFileId(
            @RequestParam("fileId")
            @NotNull(message = "文件ID不能为空")
            @Parameter(name = "fileId", description = "文件ID", required = true)
            Long fileId){
        return feignClient.deleteTradeApplicationTemplateConfirmLetterFileByFileId(fileId);
    }

    @PostMapping("/generateTemplateConfirmLetter")
    @Operation(summary = "生成交易申请确认函模板")
    @SaCheckPermission("lindi:tradeApplicationTemplateConfirmLetter:add")
    public R<?> generateTemplateConfirmLetter(
            @RequestParam("tradeApplicationId") @NotNull(message = "交易申请ID不能为空")
            Long tradeApplicationId,
            @RequestParam("confirmLetterTemplateId") @NotNull(message = "确认函模板ID不能为空")
            Long confirmLetterTemplateId,
            @RequestParam("confirmLetterType") @NotNull(message = "确认函类型不能为空")
            String confirmLetterType){
        Long createId = StpUtil.getLoginIdAsLong();
        Long tenantId = SafetyUtil.getLoginUserTenantId();
        return feignClient.generateTemplateConfirmLetter(tradeApplicationId
                , confirmLetterTemplateId, confirmLetterType, createId, tenantId);
    }
}